package com.zhou.yuefan.activity;

import com.zhou.util.ZhouHttpPostManager;
import com.zhou.yuefan.R;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NoteActivity extends Activity {
	EditText edt_note;
	EditText edt_name;
	EditText edt_contact;
	Button btn_submit;
	Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.note);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		edt_note = (EditText) findViewById(R.id.edt_note);
		edt_name = (EditText) findViewById(R.id.edt_name);
		edt_contact = (EditText) findViewById(R.id.edt_contact);
		// 获得手机imei地址
		TelephonyManager phoneManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		final String imei = phoneManager.getDeviceId();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
				edt_note.setText("");
				edt_note.invalidate();
				Toast.makeText(NoteActivity.this, "数据已上传", Toast.LENGTH_LONG).show();
					break;

				}
			}
		};

		btn_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String note = edt_note.getText().toString().trim();
				final String name = edt_name.getText().toString().trim();
				final String contact = edt_contact.getText().toString().trim();
				// 如果网络连通，则上传加载次数
				ConnectivityManager conMan = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
				boolean isWifiConnected = conMan.getNetworkInfo(
						ConnectivityManager.TYPE_WIFI).isConnected();

				boolean is3GConnected = conMan.getNetworkInfo(
						ConnectivityManager.TYPE_MOBILE).isConnected();

				if (is3GConnected || isWifiConnected) {

					new Thread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							boolean result = false;
							while (!result) {
								result = ZhouHttpPostManager.addNote(imei,
										note, contact, name);
								Log.i("zhou", "上传的循环执行了");
								try {
									Thread.sleep(3*1000);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							Log.i("zhou", "循环执行完毕");
							
							Message msg = new Message();
							msg.what = 1 ;
							handler.sendMessage(msg);
						}
					}).start();
				}
				else {
					Toast.makeText(NoteActivity.this, "网络未连接", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}
}
